package com.ilop.sthome.ui.activity.detail;

import android.annotation.SuppressLint;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.ilop.sthome.data.greenDao.DefaultBean;
import com.ilop.sthome.ui.adapter.detail.DefaultAdapter;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DefaultDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDetailHumitureBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2019-12-11.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：温湿传感器详情页
 */
public class THCheckDetailActivity extends BaseDetailActivity<ActivityDetailHumitureBinding> {

    private DefaultAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail_humiture;
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new DefaultAdapter(mContext);
        mDBind.detailList.setLayoutManager(new LinearLayoutManager(mContext));
        mDBind.detailList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        buildDefaultAutomation();
    }

    private void buildDefaultAutomation() {
        int defaultSize = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice).size();
        if (defaultSize == 0){
            DefaultDaoUtil.getInstance().setDefaultList(mContext, smartDevice.getDevCommon(), mDevice);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(TextUtils.isEmpty(mDevice.getSubdeviceName())){
            String title = getString(R.string.thcheck) + mDevice.getDevice_ID();
            mDBind.detailName.setText(title);
        }else{
            mDBind.detailName.setText(mDevice.getSubdeviceName());
        }
        doStatusShow(mDevice.getDevice_status());
        refreshDefaultList();
    }

    @SuppressLint("SetTextI18n")
    private void doStatusShow(String statusCode) {
        try {
            String signal = statusCode.substring(0,2);
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic(signal));

            String status = DeviceStatusUtil.getAlert(mContext, mDevice.getDevice_type(), statusCode);
            int statusColor = DeviceStatusUtil.getStatus(mDevice.getDevice_type(), statusCode);
            int quantity = Integer.parseInt(statusCode.substring(2,4),16);
            showDetail(statusColor, status, quantity);

            String realT;
            String realH;
            String temp = statusCode.substring(4,6);
            String humidity = statusCode.substring(6,8);
            String temp2 = Integer.toBinaryString(Integer.parseInt(temp,16));
            if (temp2.length()==8){
                realT = "-"+ (128 - Integer.parseInt(temp2.substring(1),2));
            }else{
                realT = "" + Integer.parseInt(temp2,2);
            }

            if(Integer.parseInt(realT)>100 || Integer.parseInt(realT) < -40 || Integer.parseInt(humidity,16)<0 || Integer.parseInt(humidity,16)>100){
                showDetail(3, getString(R.string.offline), 0);
            }else{
                realH = "" +Integer.parseInt(humidity,16);
                mDBind.temperature.setText(realT);
                mDBind.humidity.setText(realH);
            }
        }catch (Exception e){
            showDetail(3, getString(R.string.offline), 0);
        }
    }

    private void showDetail(int statusColor, String text, int quantity){
        if (statusColor == 3){
            mDBind.signalPosition.setImageResource(ConfigureUtil.choseSPic("00"));
            mDBind.quantityPosition.setVisibility(View.GONE);
            mDBind.quantityDesc.setVisibility(View.GONE);
        }else {
            mDBind.quantityPosition.setVisibility(View.VISIBLE);
            mDBind.quantityDesc.setVisibility(View.VISIBLE);
            mDBind.quantityPosition.setImageResource(ConfigureUtil.choseQPic(quantity));
            mDBind.quantityDesc.setText(ConfigureUtil.choseLNum(quantity));
        }
        int background = ConfigureUtil.choseDeviceStateColor(statusColor);
        mDBind.detailLayout.setBackgroundResource(background);
        mDBind.showStatus.setText(text);
    }

    @Override
    protected void refreshUI(String status) {
        super.refreshUI(status);
        doStatusShow(status);
    }

    @Override
    protected void refreshDefaultList() {
        super.refreshDefaultList();
        List<DefaultBean> defaultBeanList = DefaultDaoUtil.getInstance().findDefaultByDevice(mDevice);
        mAdapter.setList(defaultBeanList);
    }
}
